package org.vy.dal.dao;

import java.util.Date;
import java.util.List;

import org.vy.dal.QueryManager;

public class VyDataUploadJobDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE JOB_ID = m_JobId";
	private static final String FIND_MAX_REPORT_ID = "SELECT MAX(JOB_ID) as JOB_ID,null,null,null,null,null,null,null,null,null,null,null,null,null FROM <TABLE>";

	private static final String FIND_ALL = "SELECT * FROM <TABLE> ORDER BY JOB_ID DESC";
//	private static final String FIND_BY_PARAMETER = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET file_name = m_FileName, file_path_before = m_FilePathBefore, file_path_after = m_FilePathAfter, total_records = m_TotalRecords, records_success = m_RecordsSuccess, records_processed = m_RecordsProcessed, records_failed = m_RecordsFailed, records_skipped = m_RecordsSkipped, status = m_Status, job_report = m_JobReport WHERE JOB_ID = m_JobId";
	private static final String INSERT = "INSERT INTO <TABLE> (job_id, app_id, template_id, file_name, file_path_before, file_path_after, total_records, records_processed, records_success, records_failed, records_skipped, status, job_report, created_date) VALUES (m_JobId, m_AppId, m_TemplateId, m_FileName, m_FilePathBefore, m_FilePathAfter, m_TotalRecords, m_RecordsProcessed, m_RecordsSuccess, m_RecordsFailed, m_RecordsSkipped, m_Status, m_JobReport, m_CreatedDate)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE JOB_ID = m_JobId";

	public static VyDataUploadJobDO findByPK(int key) throws Exception {
		VyDataUploadJobDO dto = new VyDataUploadJobDO();
		dto.setJobId(key);
		dto = (VyDataUploadJobDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static List<VyDataUploadJobDO> findAll() throws Exception {
		VyDataUploadJobDO dto = new VyDataUploadJobDO();
		return (List<VyDataUploadJobDO>) QueryManager.executeListQuery(dto, FIND_ALL);
	}

	public static VyDataUploadJobDO insert(VyDataUploadJobDO dto) throws Exception {
		dto.setCreatedDate(new Date());
		dto.setJobId(0);
		dto = (VyDataUploadJobDO) QueryManager.executeInsert(dto, INSERT);
		VyDataUploadJobDO dto1 = (VyDataUploadJobDO) QueryManager.executeQuery(dto, FIND_MAX_REPORT_ID);
		dto.setJobId(dto1.getJobId());
		
		return dto;
	}

	public static VyDataUploadJobDO update(VyDataUploadJobDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyDataUploadJobDO delete(int key) throws Exception {
		VyDataUploadJobDO dto = new VyDataUploadJobDO();
		dto.setJobId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}
}
